﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;

namespace QLTTTH
{
    public partial class frmThongTinNhanVien : DevExpress.XtraEditors.XtraForm
    {
        public frmThongTinNhanVien()
        {
            InitializeComponent();
        }
        private void btnXoaTrang_Click(object sender, EventArgs e)
        {
            this.txtChucVu.Text = "";
            this.txtDiaChi.Text = "";
            this.txtHoTen.Text = "";
            this.txtMail.Text = "";
            this.txtMaNhanVien.Text = "";
            this.loupNhom.ResetText();
            this.txtPassword.Text = "";
            this.txtSoDienThoai.Text = "";
            this.txtTrangThai.Text = "";
            this.txtUsername.Text = "";
            this.dtNgaySinh.EditValue = DateTime.Now;
            //rdoGioiTinh.EditValue = 0;
            rdoGioiTinh.SelectedIndex = 0;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        #region-------------- Thiet lap cac thong tin len controls ---------------
        public void ThietLapThongTin(NguoiDung nv)
        {
            this.txtChucVu.Text = nv.ChucVu;
            this.txtDiaChi.Text = nv.DiaChi;
            this.txtHoTen.Text = nv.HoTen;
            this.txtMail.Text = nv.Email;
            this.txtMaNhanVien.Text = nv.MaNV.ToString();
            this.loupNhom.EditValue = nv.MaNhomND;
            this.txtPassword.Text = nv.Password;
            this.txtSoDienThoai.Text = nv.SoDT;
            this.txtTrangThai.Text = nv.TrangThai;
            this.txtUsername.Text = nv.Username;
            this.dtNgaySinh.EditValue = nv.NgaySinh.ToShortDateString();
            if (nv.GioiTinh.Equals("Nam"))
            {

                this.rdoGioiTinh.SelectedIndex = 0;
            }
            else
                this.rdoGioiTinh.SelectedIndex = 1;
        }
        #endregion

        #region ---------- Lay thong tin sinh vien tu controls thong tin sinh vien-----------
        public NguoiDung GetNguoiDung()
        {
            NguoiDung ngd = new NguoiDung();
            if (!txtMaNhanVien.Text.ToString().Equals(""))
            {
                ngd.MaNV = int.Parse(txtMaNhanVien.Text.ToString());
            }
            ngd.HoTen = txtHoTen.Text;
            ngd.DiaChi = txtDiaChi.Text;
            ngd.MaNhomND = int.Parse(loupNhom.EditValue.ToString());
            ngd.NgaySinh = DateTime.Parse(dtNgaySinh.Text.ToString());
            ngd.Password = txtPassword.Text.ToString();
            ngd.SoDT = txtSoDienThoai.Text.ToString();
            ngd.TrangThai = txtTrangThai.Text.ToString();
            if (rdoGioiTinh.SelectedIndex == 0)
                ngd.GioiTinh = "Nam";
            else
                ngd.GioiTinh = "Nữ";
            ngd.ChucVu = txtChucVu.Text;
            ngd.Email = txtMail.Text;
            //ngd.TrangThai = txtTrangThai.Text;
            ngd.Username = txtUsername.Text;

            return ngd;
        }
        #endregion


        public delegate void SendData(NguoiDung ngd);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {
                var vResult = new NhanVienManager().Validate(GetNguoiDung());
                if (vResult.Valid)
                {
                    sendData(GetNguoiDung());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            //if (sendData != null)
            //{
            //    sendData(GetNguoiDung());
            //}
            //this.Close();
        }

        private void frmThongTinNhanVien_Load(object sender, EventArgs e)
        {
            txtPassword.Properties.UseSystemPasswordChar = true;

            var dsNhom = NhomNguoiDungManager.LayDanhSachNhomNguoiDung();
            if (dsNhom.Success)
            {
                loupNhom.Properties.DataSource = dsNhom.Result;
                loupNhom.Properties.ValueMember = "MaNhomND";
                loupNhom.Properties.DisplayMember = "TenNhom";
                if (loupNhom.ItemIndex < 0)
                {
                    loupNhom.ItemIndex = 0;
                }
            }
        }
     
    }
}